package com.itheima.controller;

import com.itheima.domain.City;
import com.itheima.domain.Product;
import com.itheima.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
//城市类
@WebServlet("/city/*")
public class CityServlet extends BaseServlet {
    //查询所有
    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<City> list=city.findAll();
        //ajax获取页面数据
        String s = obj.writeValueAsString(list);
        System.out.println(list);
        resp.getWriter().write(s);
    }
    //根据id查询,回显数据到产品更新页面
    public void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //获取页面传递的City的id的值
        String cid = req.getParameter("cid");
        //获取页面的Product的id值
        String pid = req.getParameter("pid");
        //根据City的id值查询City对象
        City ct=city.findById(cid);
        //获取产品业务层的实现类的对象
        ProductServiceImpl productService = new ProductServiceImpl();
        //调用产品业务层根据id查询产品的对象
       Product pd= productService.findById(pid);
        Date departureTime = pd.getDepartureTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = formatter.format(departureTime);
        pd.setDepartureTimeStr(format);
        //将根据id查询的城市类和产品类写入页面
       req.setAttribute("pd",pd);
       req.setAttribute("ct",ct);
       //请求转发
       req.getRequestDispatcher("/pages/product-update.jsp").forward(req,resp);
    }
}
